﻿using MySql.Data.MySqlClient;
using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Quick.CodeGenerator
{
    public partial class ModifyConnectionFrm : Form
    {
        /// <summary>
        /// arg1 : 操作类型, Add Update Delete
        /// arg2 : ID
        /// </summary>
        private event Action<string, string> SaveSuccess;
        Model.DbInfo m_DbInfo = null;
        Data.DbInfo m_DbInfoDal = new Data.DbInfo();
        public ModifyConnectionFrm(Action<string, string> saveSuccess)
        {
            SaveSuccess = saveSuccess;
            InitializeComponent();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbInfo"></param>
        /// <param name="saveSuccess">
        /// arg1 : 操作类型, Add Update Delete
        /// arg2 : ID
        /// </param>
        public ModifyConnectionFrm(Model.DbInfo dbInfo, Action<string,string> saveSuccess = null)  : this(saveSuccess)
        {
            m_DbInfo = dbInfo;
            btnDelete.Enabled = m_DbInfo != null;
            if (null != m_DbInfo)
            {
                cboDataBaseType.SelectedItem = m_DbInfo.DbType;
                txtDbName.Text = m_DbInfo.DbName;
                txtDataSource.Text = m_DbInfo.DbConnectionString;
            }
        }
        private void btnTestConnection_Click(object sender, EventArgs e)
        {
            if (this.cboDataBaseType.SelectedItem == null || string.IsNullOrEmpty(txtDataSource.Text))
            {
                MessageBox.Show("请先设定数据库类型&链接字符串!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                cboDataBaseType.Focus();
                return;
            }
            try
            {
                string dbType = Convert.ToString(this.cboDataBaseType.SelectedItem);
                bool isConnection = false;
                DbConnection conn = null;
                switch (dbType)
                {
                    case "Oracle":
                        conn = new OracleConnection(txtDataSource.Text);
                        break;
                    case "MySql":
                        conn = new MySqlConnection(txtDataSource.Text);
                        break;
                    case "MsSqlServer":
                        conn = new SqlConnection(txtDataSource.Text);
                        break;
                }
                try
                {
                    conn.Open();
                    isConnection = true;
                    conn.Close();
                    conn.Dispose();
                }
                catch (Exception ex)
                {
                    isConnection = false;
                    conn.Dispose();
                }
                if (isConnection)
                {
                    MessageBox.Show("链接成功!", "链接结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("链接失败!", "链接结果", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtDataSource.Focus();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "链接错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (m_DbInfo==null)
            {
                return;
            }
            var result = MessageBox.Show("确认删除此数据库链接吗?\r\n\r\n删除后不可恢复!", "删除确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result != DialogResult.OK)
            {
                return;
            }
            int count = m_DbInfoDal.Delete(m_DbInfo);
            if (count > 0)
            {
                if (null != SaveSuccess)
                {
                    SaveSuccess.BeginInvoke("Delete",m_DbInfo.ID, null, null);
                }
                MessageBox.Show("保存成功!", "保存结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("保存失败!", "保存结果", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            cboDataBaseType.SelectedIndex = -1;
            txtDataSource.Text = "";
            txtDbName.Text = "";
        }

        private void cboDataBaseType_SelectionChangeCommitted(object sender, EventArgs e)
        {
            try
            {
                string str = Convert.ToString(cboDataBaseType.SelectedItem);
                switch (str)
                {
                    case "Oracle":
                        this.txtDataSource.Text = "User ID=用户名;Password=密码;Data Source=DNS字符串;";
                        break;
                    case "MySql":
                        this.txtDataSource.Text = "Uid=用户名;Pwd=密码;Server=服务器IP地址;Database=数据库名;";
                        break;
                    case "MsSqlServer":
                        this.txtDataSource.Text = "User Id=用户名;Password=密码;Data Source=数据库地址,端口;Initial Catalog=数据库名;";
                        break;
                }
            }
            catch (Exception)
            { }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (this.cboDataBaseType.SelectedItem == null)
            {
                MessageBox.Show("请设定数据库类型!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                cboDataBaseType.Focus();
                return;
            }
            if (string.IsNullOrEmpty(txtDbName.Text))
            {
                MessageBox.Show("请设定数据库别名!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtDbName.Focus();
                return;
            }
            if (string.IsNullOrEmpty(txtDataSource.Text))
            {
                MessageBox.Show("请设定链接字符串!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtDataSource.Focus();
                return;
            }
            Model.DbInfo model = new Model.DbInfo()
            {
                DbType = Convert.ToString(this.cboDataBaseType.SelectedItem).Trim(),
                DbConnectionString = txtDataSource.Text.Trim(),
                DbName = txtDbName.Text.Trim()
            };
            if (m_DbInfo != null)
            {
                model.ID = m_DbInfo.ID;
            }
            int count = 0;
            string operation = "";
            if (string.IsNullOrEmpty(model.ID))
            {
                model.ID = Guid.NewGuid().ToString();
                count = m_DbInfoDal.Add(model);
                operation = "Add";
            }
            else
            {
                count = m_DbInfoDal.Update(model);
                operation = "Update";
            }
            if (count > 0)
            {
                m_DbInfo = model;
                if (null != SaveSuccess)
                {
                    SaveSuccess.BeginInvoke(operation, model.ID, null, null);
                }
                MessageBox.Show("保存成功!", "保存结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("保存失败!", "保存结果", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
